<template>
  <div>
    <el-row class="nav">
      <span class="font-weight">工单状态</span>
    </el-row>
    <el-divider></el-divider>
    <el-form :inline="true"
             class="coverage-style-searchForm">
      <el-form-item>
        <div class="demo-input-suffix">
          <el-input placeholder="请输入状态名称"
                    clearable
                    prefix-icon="el-icon-search"
                    v-model="core">
          </el-input>
        </div>
      </el-form-item>
      <el-form-item>
        <button @click="search"
                type="button"
                class="el-button el-button--primary el-button--medium h30Btn ">查询</button>
      </el-form-item>
      <el-form-item class="fr">
        <button @click="createStatus({name:'新建工单状态'})"
                type="button"
                class="el-button el-button--primary el-button--medium h30Btn ">新建工单状态</button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table :data="tableData"
                v-loading='loading'
                stripe
                border
                :header-cell-style="{background:'#F2F3F8'}"
                style="width: 100%">
        <el-table-column label="状态名称"
                         align="center"
                         min-width="18%">
          <template slot-scope="scope">
            <span class="label-name">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态标识"
                         align="center"
                         min-width="18%">
          <template slot-scope="scope">
            <span v-if="scope.row.tag == 1">系统状态</span>
            <span v-else>自定义状态</span>
          </template>
        </el-table-column>
        <el-table-column label="状态排序"
                         align="center"
                         min-width="18%">
          <template slot-scope="scope">
            {{ scope.row.ordering }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间"
                         align="center"
                         min-width="18%">
          <template slot-scope="scope">
            <!-- <i class="el-icon-time"></i> -->
            <span style="margin-left: 10px">{{timestampToTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         align="center"
                         min-width="28%">
          <template slot-scope="scope">
            <el-button icon="el-icon-delete"
                       v-if="scope.row.tag === 0"
                       type="text"
                       size="mini"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
            <el-button icon="el-icon-delete"
                       disabled
                       v-else
                       type="text"
                       size="mini">删除
            </el-button>
            <el-button icon="el-icon-edit"
                       type="text"
                       size="mini"
                       @click="handleEdit(scope.$index, scope.row,{name:'编辑工单状态'})">编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="paging-foot">
      <el-pagination background
                     class="el-pagination page-foot-spacing el-pagination-style"
                     v-show="totalDataLength > 0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="totalDataLength"></el-pagination>
    </el-row>
  </div>
</template>

<script>
import { deleteWorkOrderStatus, selectByNameNotContainNotNoChanging } from '@/api/workStatus.js'
import common from '@/common/common.js'
// import { mapState } from 'vuex'
// tag===0时自己配置的， tag>0时系统配置
export default {
  data () {
    return {
      loading: true,
      totalDataLength: 0,
      tableData: [],
      core: '',
      pageSize: 10,
      page: 1
    }
  },
  mounted () {
    this.search()
  },
  methods: {
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.page = 1
      this.searchData()
    },
    handleCurrentChange (page) {
      this.page = page
      this.searchData()
    },
    // 时间戳的转换
    timestampToTime (timestamp) {
      return common.timestampToTime(timestamp)
    },
    handleEdit (index, row, params) {
      this.$store.commit('workStatusIdFun', row.id)
      this.$store.commit('systemConfigTitFun', params)
      this.$router.push('/createStatus')
    },
    handleDelete (index, row) {
      this.$confirm('确定要删除吗?', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        customClass: 'messageBox-prompt-delete'
        // type: 'warning'
      }).then(() => {
        deleteWorkOrderStatus(row.id).then(res => {
          let data = res.data
          if (data.retCode === 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.search()
          } else {
            this.$message({
              message: data.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      })
    },
    createStatus (params) {
      this.$store.commit('workStatusIdFun', '')
      this.$store.commit('systemConfigTitFun', params)
      this.$router.push('/createStatus')
    },
    search () {
      this.page = 1
      this.searchData()
    },
    // 封装
    searchData () {
      let params = {
        name: this.core,
        currentPage: this.page,
        pageSize: this.pageSize
      }
      selectByNameNotContainNotNoChanging(params).then(res => {
        let data = res.data
        if (data.retCode === 200) {
          this.tableData = data.data
          this.loading = false
          this.totalDataLength = data.count
        }
      })
    }
  },
  computed: {
    // ...mapState({
    //   workStatusId: 'workStatusId'
    // })
    // ...mapState(['workStatusId'])
  }
}
</script>
<style scoped>
.coverage-style-searchForm >>> .el-input__inner {
  width: 300px;
}

.label-name {
  color: #2393f1;
}

>>> .el-button > i {
  font-size: 13px;
  margin-right: 10px;
}

.el-pagination {
  text-align: right;
  padding: 20px 0;
}

>>> .el-input__icon {
  font-size: 13px;
  font-weight: 800;
}

>>> .el-input--prefix .el-input__inner {
  padding-left: 35px;
}
</style>
